import { app, BrowserWindow ,ipcMain} from 'electron';
import WinState from 'electron-win-state'
import path from 'path'
import fs from 'fs'
const winState = WinState.default
function createWindow() {
    const mainWindowState = new winState({
        defaultWidth: 800,
        defaultHeight: 600,        
    });

    const win = new BrowserWindow({
        ...mainWindowState,        
        autoHideMenuBar: true,
        show:false,//默认不显示
        webPreferences: {
            preload: path.join(process.cwd(),'/src/electron/preload/', 'index.js'),
        },
    })
    // 监听事件
    ipcMain.on('save-file',(_,data)=>{
        fs.writeFileSync(path.join(process.cwd(),'/src/electron/message/data.json'),JSON.stringify(data))
    })
    // 派发事件
    ipcMain.handle('get-file',()=>{
        const res = fs.readFileSync(path.join(process.cwd(),'/src/electron/message/data.json'),{encoding:'utf-8'})
        return res
    })
    win.loadURL(' http://localhost:5173/');
    win.webContents.openDevTools();//控制台
    win.on('ready-to-show', () => {
        //内容加载后展示窗口
        win.show()
    })
    win.setMenu(null)
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})